import { useI18n } from '@/hooks/web/useI18n'
import { reactive, UnwrapRef } from 'vue'
import { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { useValidator } from '@/hooks/web/useValidator'

const { t } = useI18n()
import { ActionButtonsTemplate } from '@/api/global/types'

const { required } = useValidator()

interface Option {
  label: string
  value: string | number // 具体取决于你的数据
}

const createOptions = (enumObject: { [key: string]: string }): Option[] => {
  return Object.keys(enumObject).map((key) => ({
    label: enumObject[key],
    value: parseInt(key, 10)
  }))
}

export default function (
  actionButtonsTemplate?: ActionButtonsTemplate,
  supplierList?: UnwrapRef<any>,
  fieldEnum?: any,
  tableDialogStoreID?: string,
  tableDialogCurdSchema?: CrudSchema[],
  operatorList?: UnwrapRef<any>,
  examine?: UnwrapRef<any>,
  warehouseList?: UnwrapRef<any>,
  futures?: UnwrapRef<any>
) {
  const paytypeOption = reactive(createOptions(fieldEnum?.pay_type || {}))
  const paytermsOption = reactive(createOptions(fieldEnum?.pay_terms || {}))

  const crudSchemas = reactive<CrudSchema[]>([
    {
      field: 'selection',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      },
      table: {
        type: 'selection'
      }
    },
    {
      field: 'id',
      label: t('purchase.id'),
      align: 'center',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'supid',
      label: t('purchase.sup'),
      search: {
        hidden: true
      },
      form: {
        component: 'Select',
        componentProps: {
          options: supplierList,
          disabled: true
        },
        formItemProps: {
          rules: [required()]
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.supid_value
          }
        }
      },
      detail: {
        slots: {
          default: (data: any) => {
            return data.supid_value
          }
        }
      }
    },
    {
      field: 'purchase_no',
      label: t('purchase.purchase_no'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        formItemProps: {},
        componentProps: {
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'sales_orderno',
      label: t('purchase.sales_orderno'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        formItemProps: {},
        componentProps: {
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'order_time',
      label: t('purchase.order_time'),
      search: {
        hidden: true
      },
      form: {
        component: 'DatePicker',
        componentProps: {
          type: 'date',
          valueFormat: 'YYYY-MM-DD',
          disabled: true
        },
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'signatoryid',
      label: t('purchase.signatory'),
      search: {
        hidden: true
      },
      form: {
        component: 'Select',
        formItemProps: {
          rules: [required()]
        },
        componentProps: {
          options: operatorList,
          // multiple: true,
          filterable: true,
          limit: 1,
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.signatoryid_value
          }
        }
      },
      detail: {
        slots: {
          default: (data: any) => {
            return data.signatoryid_value
          }
        }
      }
    },
    {
      field: 'pay_time',
      label: t('purchase.pay_time'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        formItemProps: {},
        componentProps: {
          disabled: true
        },
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'pay_type',
      label: t('purchase.pay_type'),
      search: {
        hidden: true
      },
      form: {
        component: 'Select',
        formItemProps: {
          rules: [required()]
        },
        componentProps: {
          options: paytypeOption,
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.pay_type_value
          }
        }
      },
      detail: {
        slots: {
          default: (data: any) => {
            return data.pay_type_value
          }
        }
      }
    },
    {
      field: 'pay_terms',
      label: t('purchase.pay_terms'),
      search: {
        hidden: true
      },
      form: {
        component: 'Select',
        formItemProps: {
          rules: [required()]
        },
        componentProps: {
          options: paytermsOption,
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.pay_terms_value
          }
        }
      },
      detail: {
        slots: {
          default: (data: any) => {
            return data.pay_terms_value
          }
        }
      }
    },
    {
      field: 'advance_delivery_date',
      label: t('purchase.advance_delivery_date'),
      search: {
        hidden: true
      },
      form: {
        component: 'DatePicker',
        componentProps: {
          type: 'date',
          valueFormat: 'YYYY-MM-DD',
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'delivery_date',
      label: t('purchase.delivery_date'),
      search: {
        hidden: true
      },
      form: {
        component: 'DatePicker',
        componentProps: {
          type: 'date',
          valueFormat: 'YYYY-MM-DD',
          disabled: futures == 0,
          disabledDate: (date: any) => {
            return date.valueOf() < new Date().valueOf()
          }
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'warehouse',
      label: t('purchase.warehouse'),
      search: {
        hidden: true
      },
      form: {
        component: 'TreeSelect',
        componentProps: {
          data: warehouseList,
          filterable: true,
          multiple: futures == 2
        },
        formItemProps: {
          rules: [required()]
        }
      },
      table: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'ccname_ids',
      label: '可售国家地区',
      search: {
        hidden: true
      },
      form: {
        component: 'TreeSelect',
        componentProps: {
          data: warehouseList,
          filterable: true,
          multiple: futures == 2
        },
        formItemProps: {
          rules: [required()]
        }
      },
      table: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'arrival_batch',
      label: '物流批次',
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        formItemProps: {
          rules: [
            {
              required: true,
              trigger: ['blur'],
              validator: (rule: Record<string, any>, value: string, callback: Function) => {
                if (!value) {
                  callback(new Error(t('purchase.batchValMsg')))
                } else {
                  if (/^[A-Za-z0-9-_]{6,}$/.test(value)) {
                    callback()
                  } else {
                    callback(new Error(t('purchase.batchValidation')))
                  }
                }
              }
            }
          ]
        }
      },
      table: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'pay_num',
      label: t('purchase.pay_num'),
      search: {
        hidden: true
      },
      form: {
        component: 'InputNumber',
        formItemProps: {},
        componentProps: {
          disabled: true
        },
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'futures',
      label: t('purchase.entry_type'),
      search: {
        hidden: true
      },
      form: {
        component: 'Select',
        componentProps: {
          options: [
            {
              label: '现货',
              value: 0
            },
            {
              label: '在途期货',
              value: 1
            },
            {
              label: '大期货',
              value: 2
            }
          ],
          disabled: true
        },
        value: futures,
        formItemProps: {
          rules: [required()]
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.supid_value
          }
        }
      },
      detail: {
        slots: {
          default: (data: any) => {
            return data.supid_value
          }
        }
      }
    },
    {
      field: 'purchase_goods',
      label: t('purchase.purchase_goods'),
      search: {
        hidden: true
      },
      form: {
        colProps: {
          span: 24
        },
        component: 'EntryFrom',
        formItemProps: {
          rules: [required()]
        },
        componentProps: {
          tableDialogStoreID: tableDialogStoreID,
          tableDialogCurdSchema: tableDialogCurdSchema,
          warehouseList: warehouseList,
          disabled: true,
          futures: futures
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'product_total_amount',
      label: t('purchase.total_amount'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        formItemProps: {},
        componentProps: {
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'other_amount',
      label: t('purchase.other_amount'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        formItemProps: {},
        componentProps: {
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'pay_amount',
      label: t('purchase.pay_amount'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        componentProps: {
          disabled: true
        },
        formItemProps: {}
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'pay_status',
      label: t('purchase.pay_status'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'creator',
      label: t('purchase.creator'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.creator_value
          }
        }
      }
    },
    {
      field: 'create_time',
      label: t('purchase.create_time'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'updater',
      label: t('purchase.updater'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.updater_value
          }
        }
      }
    },
    {
      field: 'update_time',
      label: t('purchase.update_time'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'notes',
      label: t('purchase.notes'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        formItemProps: {},
        componentProps: {
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'status',
      label: t('purchase.status'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.status_value
          }
        }
      }
    },
    {
      field: 'owner',
      label: t('purchase.owner'),
      search: {
        hidden: true
      },
      form: {
        component: 'Select',
        formItemProps: {
          rules: [required()]
        },
        componentProps: {
          options: operatorList,
          // multiple: true,
          filterable: true,
          limit: 1,
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.owner_value
          }
        }
      }
    },
    {
      field: 'partner',
      label: t('purchase.partner'),
      search: {
        hidden: true
      },
      form: {
        component: 'Select',
        formItemProps: {
          // rules: [required()]
        },
        componentProps: {
          options: operatorList,
          // multiple: true,
          filterable: true,
          limit: 1,
          disabled: true
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.partner_value
          }
        }
      }
    },
    {
      field: 'info',
      label: t('purchase.info'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        formItemProps: {},
        hidden: !examine
      },
      table: {
        hidden: true
      }
    },
    {
      field: 'action',
      width: (actionButtonsTemplate?.length || 3) * 60 + 20 + 'px',
      label: t('tableDemo.action'),
      fixed: 'right',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return actionButtonsTemplate?.template(data) ?? null
          }
        }
      }
    }
  ])

  return {
    crudSchemas
  }
}
